package homework12月15号.homework2;

import cn.itcast.xml.XmlUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * Created by dingw on 2019/12/11.
 */
public class Demo {
   static List<User> ulist = XmlUtils.readAll("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\user.xml",User.class);
   static List<Product> plist = XmlUtils.readAll("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\product.xml", Product.class);
   static List<ProductDetails> pdlist = XmlUtils.readAll("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\productDetails.xml", ProductDetails.class);
   static List<Category> clist = XmlUtils.readAll("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\category.xml", Category.class);
    static Scanner sc = new Scanner(System.in);;
    static int aa=0;
    static  String bb="";
    static  int cc=0;
    public static void main(String[] args) throws IOException {
        while (true){
            run();
            System.out.println("========================欢迎来到淘宝商店！==========================");
            System.out.println("====================================================================");
            System.out.println("【1】注册");
            System.out.println("【2】登录");
            System.out.println("【3】查看商品");
            System.out.println("【4】修改密码");
            System.out.println("【5】退出商店");
            System.out.println("请输入：");
            int a = sc.nextInt();
            switch (a){
                case 1:
                    zc();
                    break;
                case 2:
                    dl();
                    break;
                case 3:
                    ck();
                    break;
                case 4:
                    gmm();
                    break;
                case 5:
                    System.out.println("退出成功！欢迎下次光临！！！");
                    return;
                    default:
                        System.out.println("输入错误！请重新输入");
            }
        }
    }
    /*购买*/
    private static void gm() throws IOException {
        if (aa==2){

        }else {
            System.out.println("用户请登录...");
            dl();
        }
    }
   /*id信息*/
    public static void idxx() throws IOException {
        System.out.println("1 手机");
        System.out.println("2 电脑");
        System.out.println("请选择需要查看的商品");
        int a = sc.nextInt();
        String str=a+"";
        switch (a){
            case 1:
                System.out.println("id             名称                     颜色               存储容量     数量     价格");
                for (Category c : clist) {
                    if (c.getId().equals(str)){
                        for (Product p : plist) {
                            for (ProductDetails pd : pdlist) {
                                if (p.getId().equals(pd.getProductId())&&p.getCid().equals(str)){
                                    System.out.println(pd.getId()+"                        "+p.getPname()+"      "+pd.getColor()+"      "+pd.getMemory()+"      "+pd.getNum()+"      "+pd.getPrice());
                                }
                            }
                        }
                    }
                }
                gwc();
                break;
            case 2:
                System.out.println("id             名称                     颜色               存储容量     数量     价格");
                for (Category c : clist) {
                    if (c.getId().equals(str)){
                        for (Product p : plist) {
                            for (ProductDetails pd : pdlist) {
                                if (p.getId().equals(pd.getProductId())&&p.getCid().equals(str)){
                                    System.out.println(pd.getId()+"                        "+p.getPname()+"      "+pd.getColor()+"      "+pd.getMemory()+"      "+pd.getNum()+"      "+pd.getPrice());
                                }
                            }
                        }
                    }
                }
                gwc();
                break;
            default:
                System.out.println("输入错误！");
        }
    }
    /*购物车*/
    public static void gwc() throws IOException {
        while (true){
            System.out.println("【1】添加到购物车");
            System.out.println("【2】修改购物车指定商品数量");
            System.out.println("【3】删除购物车中指定商品");
            System.out.println("【4】结算购物车");
            System.out.println("【5】退出");
            System.out.println("请输入：");
            int a = sc.nextInt();
            switch (a){
                case 1:
                    tjgwc();
                    break;
                case 2:
                    xgsl();
                    break;
                case 3:
                    scsp();
                    break;
                case 4:
                    gm();
                    break;
                case 5:
                    System.out.println("退出成功！");
                  return;
                  default:
                      System.out.println("输入有误！");
            }
        }
    }
    /*删除商品*/
    public static void scsp() throws IOException {
        FileUtils.writeStringToFile(new File("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\cart.txt"),"\n",false);
    }
   /*修改商品*/
    public static void xgsl() throws IOException {
        List<String> list = FileUtils.readLines(new File("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\cart.txt"));
        System.out.println(list.get(list.size()-1));
        String[] arr = list.get(list.size()-1).split(";");
        System.out.println(Arrays.toString(arr));
        System.out.println("请输入修改的数量：");
        String str = sc.next();
        cc=Integer.parseInt(str);
        String s="";
        for (int i = 0; i < arr.length; i++) {
            if (i==5){
                s+=str+",";
            }else {
                s+=arr[i];
            }
        }
        System.out.println("修改成功");
        FileUtils.writeStringToFile(new File("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\cart.txt"),s+"\n",false);
    }
    /*添加购物车*/
    public static void tjgwc() throws IOException {
        System.out.println("请输入商品id：");
        String str = sc.next();
        for (ProductDetails pd : pdlist) {
            if (pd.getId().equals(str)){
                for (Product p : plist) {
                    if (p.getId().equals(pd.getProductId())){
                        System.out.println("购买数量：");
                        String str1 = sc.next();
                        String str2="用户名;"+bb+",商品id;"+p.getId()+",商品名称;"+p.getPname()+",商品数量;"+str1+",商品价格;"+pd.getPrice();
                        FileUtils.writeStringToFile(new File("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\cart.txt"),str2+"\n",false);
                        System.out.println("添加成功！");
                        return;
                    }
                }
            }
        }
        System.out.println("没有此商品");
    }
    /*商品信息*/
    public static void spxx() {
        System.out.println("名称                     颜色               存储容量     数量     价格");
        for (Product p : plist) {
            for (ProductDetails pd : pdlist) {
                if (p.getId().equals(pd.getProductId())){
                    System.out.println(p.getPname()+"      "+pd.getColor()+"      "+pd.getMemory()+"      "+pd.getNum()+"      "+pd.getPrice());
                }
            }
        }
    }
    /*类别信息*/
    public static void lbxx() throws IOException {
        idxx();
    }
    /*查看商品*/
    public static void ck() throws IOException {
        System.out.println("===========================================================");
        System.out.println("淘宝商城欢迎您！");
        while (true){
                    System.out.println("【1】查看商品类别信息");
                    System.out.println("【2】查看商品信息");
                    System.out.println("【3】退出");
                    System.out.println("请输入：");
                    int a = sc.nextInt();
                    switch (a){
                        case 1:
                            lbxx();
                            break;
                        case 2:
                            spxx();
                            break;
                        case 3:
                            System.out.println("退出成功！");
                            return;
                        default:
                            System.out.println("输入不正确");
                    }
                }
        }
    /*改密码*/
    public static void gmm() {
        System.out.println("请输入用户名");
        String str1 = sc.next();
        for (User s : ulist) {
            if (s.getUsername().equals(str1)){
                System.out.println("请输入绑定的手机号");
                String str2 = sc.next();
                for (User u : ulist) {
                    if (u.getPhone().equals(str2)){
                        System.out.println("请输入密码");
                        String str3 = sc.next();
                        for (User us : ulist) {
                            if (us.getPassword().equals(str3)){
                                System.out.println("请输入修改密码");
                                String str4 = sc.next();
                                us.setPassword(str4);
                                System.out.println(  us.getPassword());
                                int a=0;
                                for (User user : ulist) {
                                    a++;
                                    if (a==1){
                                        XmlUtils.write("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\user.xml",user,false);
                                    }else {
                                        XmlUtils.write("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\user.xml",user,true);
                                    }
                                }
                                System.out.println("修改密码成功！");
                                return;
                            }
                        }
                        System.out.println("密码错误！");
                        return;
                    }
                }
                System.out.println("手机号错误...");
                return;
            }
        }
        System.out.println("没有该用户，请注册...");
    }
    /*登录*/
    public static void dl() throws IOException {
        System.out.println("请输入用户名！");
        String str1 = sc.next();
        for (User u : ulist) {
            if (u.getUsername().equals(str1)){
                bb=str1;
                System.out.println("请输入密码：");
                String str2 = sc.next();
                for (User s : ulist) {
                    if (s.getPassword().equals(str2)){
                        System.out.println("请输入手机号：");
                        String str3 = sc.next();
                        for (User s1 : ulist) {
                            if (s1.getPhone().equals(str3)){
                                System.out.println("登录成功！");
                                aa=2;
                                ck();
                                return;
                            }
                        }
                        System.out.println("手机号不正确");
                        return;
                    }
                }
                System.out.println("密码错误！");
                return;
            }
        }
        System.out.println("该用户不存在！");
    }
    /*注册*/
    public static void zc() {
            System.out.println("请输入用户名：");
            String str1 = sc.next();
            System.out.println("请输入手机号：");
            String str2 = sc.next();
            for (User u : ulist) {
                if (u.getUsername().equals(str1)){
                    System.out.println("用户名已存在，请重新注册");
                    return;
                }else if (u.getPhone().equals(str2)){
                    System.out.println("该手机号已被注册，请重新选择其他手机号");
                    return;
                }
            }
            System.out.println("请输入密码：");
            String str3 = sc.next();
            System.out.println("请输入存入钱数：");
            double str4 = sc.nextDouble();
            int size = ulist.size();
            XmlUtils.write("D:\\ldea dashuju\\day.12.12\\src\\czxy\\homework2\\user.xml",new User((size + 1)+"", str1, str3, str2,"普通用户", str4),true);
        System.out.println("注册成功！");
    }
    /*初始化数据*/
    public static void run() {
        for (Category c : clist) {
            for (Product s : plist) {
                if (c.getId().equals(s.getCid())){
                    c.getProductList().add(s);
                }
            }
        }
        for (Category c : clist) {
            for (Product s : plist) {
                if (c.getId().equals(s.getCid())){
                    s.setCategory(c);
                }
            }
        }
    }
}